package com.atues.kuangshengjuc.lock8;

public class Test1 {
    public static void main(String[] args) {
        Phone phone = new Phone();
        new Thread(() -> {
            try {
                phone.sendMsg();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, "A").start();
//        必须会先执行完sendMsg()方法才能执行call()方法，因为方法锁了对象

        new Thread(() -> {
            try {
                phone.call();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, "B").start();
    }
}

class Phone{
    //方法上锁的是对象锁
    public synchronized void sendMsg() throws Exception {
        System.out.println("发短信");
    }
    public synchronized void call() throws Exception {
        System.out.println("打电话");
    }
}
